<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	session.setAttribute("basePath", basePath);
%>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="${basePath }">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<title>登录中</title>
</head>
<body>
	<h1>正在登录中...</h1>
	<p>登录之后才能继续操作。</p>

	<a style="display: none;" id="go_to_button" href="${param.go_to_page }">点击进入</a>
	<!-- BEGIN CORE PLUGINS -->
	<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
	<script src="https://cdn.bootcss.com/jquery-migrate/1.2.1/jquery-migrate.min.js" type="text/javascript"></script>
	<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	<script type="text/javascript">
		$(function()
		{
			$.post("wc_get_wechat_info",
			{
				"currentUrl" : location.href.split('#')[0]
			}, function(json)
			{
				wx.config(
				{
					debug : false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId : json.appId, // 必填，公众号的唯一标识
					timestamp : json.timestamp, // 必填，生成签名的时间戳
					nonceStr : json.nonceStr, // 必填，生成签名的随机串
					signature : json.signature,// 必填，签名，见附录1
					jsApiList : [ 'onMenuShareTimeline', 'chooseWXPay' ]
				// 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				wx.ready(function()
				{
					wx.onMenuShareTimeline(
					{
						title : '', // 分享标题
						link : '', // 分享链接
						imgUrl : '', // 分享图标
						success : function()
						{
							// 用户确认分享后执行的回调函数
							alert("成功分享了");
						},
						cancel : function()
						{
							// 用户取消分享后执行的回调函数
							alert("取消分享了");
						}
					});
				});
				wx.error(function(res)
				{
				});

				if ("${buyerSession.buyerId}".length > 0)
				{
					//$("#go_to_button").show();
					//alert("2.param.go_to_page=" + "${param.go_to_page}");
					window.location.href = "${param.go_to_page}";
				}

				var code = "${param.code}";
				//alert("code=" + code);
				if (code.length > 0)
				{
					$.post("wc_get_user_info",
					{
						'code' : code
					}, function(return_json)
					{
						if (return_json.success == true)
						{
							//alert("openid(getUserInfo):" + return_json.openid);
							//$("#go_to_button").show();
							//alert("3.param.go_to_page=" + "${param.go_to_page}");
							window.location.href = "${param.go_to_page}";
							//window.location.reload(true);
						}
					},
					//返回类型
					"json");
				}
				else
				{
					$.post("wc_valid_have_this_person", {}, function(valid_json)
					{
						if (valid_json.success == false)
						{

							var authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

							authUrl = authUrl.replace("APPID", json.appId);
							authUrl = authUrl.replace("REDIRECT_URI", location.href.split('#')[0]);
							authUrl = authUrl.replace("SCOPE", "snsapi_base");
							authUrl = authUrl.replace("STATE", 1);
							window.location.href = authUrl;
						}
						else
						{
							//alert("openid(validHaveThisPerson):" + valid_json.openid);
							//alert("1.param.go_to_page=" + "${param.go_to_page}");
							window.location.href = "${param.go_to_page}";
						}
					},
					//返回类型
					"json");
				}

			},
			//返回类型
			"json");

			//解决浏览器缓存
			function timestamp(url)
			{
				var getTimestamp = new Date().getTime();
				if (url.indexOf("?") > -1)
				{
					url = url + "&timestamp=" + getTimestamp
				}
				else
				{
					url = url + "?timestamp=" + getTimestamp
				}
				return url;
			}

		});
	</script>
</body>
</html>